﻿namespace NEvoWeb.Modules.NB_Store
{
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;

    public class LogText : Component
    {
        private System.ComponentModel.Container components;
        private const string DEFAULT_APP_NAME = "TextLogger";
        private string psAppName;
        private string psFileName;

        public LogText()
        {
            this.InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
        }

        [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
        public void Log(string TextValue)
        {
            try
            {
                StreamWriter writer;
                string path = this.ResolveFileName();
                if (!File.Exists(path))
                {
                    writer = File.CreateText(path);
                }
                else
                {
                    writer = File.AppendText(path);
                }
                try
                {
                    writer.WriteLine(TextValue);
                    writer.Close();
                }
                catch (Exception exception1)
                {
                    ProjectData.SetProjectError(exception1);
                    Exception exception = exception1;
                    Information.Err().Raise(Information.Err().Number, null, exception.Message, null, null);
                    ProjectData.ClearProjectError();
                }
            }
            catch (Exception exception3)
            {
                ProjectData.SetProjectError(exception3);
                Exception exception2 = exception3;
                ProjectData.ClearProjectError();
            }
        }

        public void LogError(Exception Ex, string ProcedureName = "")
        {
            string textValue = "Exception " + Ex.Message + " occurred";
            if (ProcedureName != "")
            {
                textValue = textValue + " in " + ProcedureName;
            }
            textValue = textValue + " [" + DateAndTime.Now.ToLongDateString() + "]";
            this.Log(textValue);
        }

        public void Reset()
        {
            string path = this.ResolveFileName();
            try
            {
                File.Delete(path);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                ProjectData.ClearProjectError();
            }
        }

        private string ResolveFileName()
        {
            string currentDirectory = Environment.CurrentDirectory;
            if (Strings.Right(currentDirectory, 1) != @"\")
            {
                currentDirectory = currentDirectory + @"\";
            }
            string str2 = currentDirectory + "Log.txt";
            if (this.psFileName == "")
            {
                return str2;
            }
            if (!File.Exists(this.psFileName))
            {
                string psFileName;
                try
                {
                    FileInfo info = new FileInfo(this.psFileName);
                    string directoryName = info.DirectoryName;
                    if (!Directory.Exists(directoryName))
                    {
                        Directory.CreateDirectory(directoryName);
                        psFileName = this.psFileName;
                    }
                    psFileName = this.psFileName;
                }
                catch (Exception exception1)
                {
                    ProjectData.SetProjectError(exception1);
                    Exception exception = exception1;
                    psFileName = str2;
                    ProjectData.ClearProjectError();
                }
                return psFileName;
            }
            return this.psFileName;
        }

        public bool WriteToEventLog(string Entry, EventLogEntryType EventType = EventLogEntryType.Information)
        {
            bool flag;
            EventLog log = new EventLog();
            string source = Interaction.IIf(Strings.Trim(this.psAppName) == "", "TextLogger", this.psAppName).ToString();
            try
            {
                if (!EventLog.SourceExists(source))
                {
                    EventLog.CreateEventSource(source, "Application");
                }
                log.Source = source;
                log.WriteEntry(Entry, EventType);
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                flag = false;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public string AppName
        {
            get
            {
                return this.psAppName;
            }
            set
            {
                this.psAppName = value;
            }
        }

        public string FileName
        {
            get
            {
                return this.ResolveFileName();
            }
            set
            {
                this.psFileName = value;
            }
        }
    }
}

